import React from 'react'
import { Empty } from 'antd'
import useEditorAPI from '../EditorToolbar/useEditorAPI'
import HttpDataSourcePanel from './components/HttpDataSourcePanel'
import TransformPanel from './components/TransformPanel'
import ValidationPanel from './components/ValidationPanel'
import InvocationPanel from './components/InvocationPanel'
import HttpCallPanel from './components/HttpCallPanel'
import ExclusivePanel from './components/ExclusivePanel'
import { uuid } from '@/utils/utils'

function ForNode({ node, input, updateNode }) {
  const { nodeType, nodeCode, id } = node

  const editorAPI = useEditorAPI()
  const { getNodes } = editorAPI

  // if (getNodes().some(each => each.nodeCode === nodeCode && each.id !== id)) {
  //   const sameTypeNodeCount = getNodes().filter(each => each.nodeType === nodeType).length;
  //   updateNode({
  //     nodeCode: sameTypeNodeCount >= 2 ? `${nodeCode}_${sameTypeNodeCount}` : nodeCode,
  //   });
  // }

  if (nodeCode === undefined)
    updateNode({ nodeCode: uuid() })

  // HTTP数据源节点
  if (nodeType === 'HttpDataSource')
    return <HttpDataSourcePanel node={node} {...editorAPI} />

  // 转换器节点
  if (nodeType === 'Transform')
    return <TransformPanel node={node} input={input} {...editorAPI} />

  // 校验器节点
  if (nodeType === 'Validation')
    return <ValidationPanel node={node} {...editorAPI} />

  // 本地调用
  if (nodeType === 'Invocation')
    return <InvocationPanel node={node} {...editorAPI} />

  // HTTP调用
  if (nodeType === 'HttpCall')
    return <HttpCallPanel node={node} {...editorAPI} />

  // 互斥网关
  if (nodeType === 'ExclusiveGateway')
    return <ExclusivePanel node={node} {...editorAPI} />

  return <Empty />
}

export default ForNode
